package com.yuerlive.rainbow.module.main.fragment;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.base.BaseFragment;
import com.yuerlive.rainbow.common.FragmentPagerAdapterImpl;
import com.yuerlive.rainbow.common.OnPagerChangeListenerAdapter;
import com.yuerlive.rainbow.module.main.MainActivity;
import com.yuerlive.rainbow.module.main.mainInterface.MainFragmentListener;
import com.yuerlive.rainbow.module.search.SearchActivity;
import com.yuerlive.rainbow.utils.DeviceUtils;
import com.yuerlive.rainbow.utils.L;
import com.yuerlive.rainbow.widget.behavior.FooterBehavior;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xiaoyi on 2017/5/17.
 */

public class FragmentMain extends BaseFragment implements MainFragmentListener, FooterBehavior.Callback {
    @BindView(R.id.vpFragment)
    ViewPager vpFragment;
    @BindView(R.id.tabFollow)
    TextView tabFollow;
    @BindView(R.id.tabHot)
    TextView tabHot;
    @BindView(R.id.rlTopBar)
    RelativeLayout rlTopBar;
    @BindView(R.id.statusBar)
    View statusBar;

    FragmentPagerAdapterImpl adapter;
    private boolean isShow = true;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main;
    }

    @Override
    public void initViews(View root) {
        adapter = new FragmentPagerAdapterImpl(getChildFragmentManager());
        FragmentFollowed fragmentFollowed = new FragmentFollowed();
        fragmentFollowed.setFragmentMainListener(this);
        adapter.addItem(fragmentFollowed);
        FragmentHot fragmentHot = new FragmentHot();
        adapter.addItem(fragmentHot);
        vpFragment.setAdapter(adapter);
        vpFragment.addOnPageChangeListener(new OnPagerChangeListenerAdapter() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                changeTab(position);
            }
        });
        vpFragment.setCurrentItem(1);

        FooterBehavior.setCallback(this);
    }

    private void changeTab(int pos) {
        if (pos == 0) {
            tabFollow.setTextAppearance(mActivity, R.style.text_home_tab_selected);
            tabFollow.setBackgroundResource(R.drawable.tab_selected);
            tabHot.setTextAppearance(mActivity, R.style.text_home_tab_unselected);
            tabHot.setBackgroundResource(R.color.transparent);
        } else {
            tabHot.setTextAppearance(mActivity, R.style.text_home_tab_selected);
            tabFollow.setTextAppearance(mActivity, R.style.text_home_tab_unselected);
            tabHot.setBackgroundResource(R.drawable.tab_selected);
            tabFollow.setBackgroundResource(R.color.transparent);
        }
    }

    @OnClick({R.id.search, R.id.tabFollow, R.id.tabHot})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.search:
                jumpToActivity(SearchActivity.class);
                break;
            case R.id.tabHot:
                changeTab(1);
                vpFragment.setCurrentItem(1, true);
                break;
            case R.id.tabFollow:
                changeTab(0);
                vpFragment.setCurrentItem(0, true);
                break;
        }
    }

    public void hide() {
        rlTopBar.animate()
                .translationY(-(rlTopBar.getHeight() + statusBar.getHeight()))
                .setInterpolator(new AccelerateInterpolator(2));
        isShow = false;
    }

    public void show() {
        rlTopBar.animate()
                .translationY(0)
                .setInterpolator(new DecelerateInterpolator(2));
        isShow = true;
    }

    public boolean isShow() {
        return isShow;
    }

    @Override
    public void hideTopBar() {
     /*   L.e(TAG,"hideTopBar");
      if(isShow()){
          hide();
      }*/
    }

    @Override
    public void showTopBar() {
        /*L.e(TAG,"showTopBar");
        if(!isShow()){
           show();
        }*/
    }

    @Override
    public void hideBottomBar() {

    }

    @Override
    public void showBottomBar() {

    }

    @Override
    public boolean isShown() {
        return isShow();
    }

    @Override
    public void onCallback(int translationY) {
        if (tobBarRealHeight <= 0) {
            tobBarRealHeight = rlTopBar.getHeight() + DeviceUtils.deviceStatusHeight(mActivity);
        }
        float height = tobBarRealHeight;
        float barHeight = DeviceUtils.dp2px(mActivity, 50);
        int realTranslationY = (int) (-translationY * (height / barHeight));
        rlTopBar.setTranslationY(realTranslationY);
        setTopTranslationY(realTranslationY);
    }

    private int topTranslationY;

    private int tobBarRealHeight;

    public void setTopTranslationY(int y) {
        this.topTranslationY = y;
    }

    public int getTobarRealHeight() {
        return tobBarRealHeight;
    }

    public int getTopTranslationY() {
        return topTranslationY;
    }

    public void doTopTranslate(int topTranslationY){
//        rlTopBar.setTranslationY(topTranslationY);
        ObjectAnimator animator;
        if (topTranslationY==0){
            animator = ObjectAnimator.ofFloat(rlTopBar,"translationY",getTopTranslationY(),topTranslationY);
        }else {
            animator = ObjectAnimator.ofFloat(rlTopBar,"translationY",getTopTranslationY(),-topTranslationY);
        }

        animator.setDuration(200);
        animator.start();
        setTopTranslationY(topTranslationY);
        ((MainActivity)mActivity).doBottomTranslationY(topTranslationY);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        FooterBehavior.removeCallback();
    }
}
